package org.loofer.uiframework.navigator.backstack;

import android.view.KeyEvent;
import org.loofer.ext.Ext;

public class FragmentBackHelper implements IFragmentBackHelper {
    private FragmentBackStackManager mChildFragmentBackStackManager;
    private AbstractFragment mCurrentChildFragment;
    private AbstractFragment mFragment;
    private FragmentBackStackManager mParentFragmentBackStackManager;

    public FragmentBackHelper(AbstractFragment fragment) {
        this.mFragment = fragment;
    }

    public void clearChildFragmentBackStackManager() {
        if (this.mChildFragmentBackStackManager != null) {
            this.mChildFragmentBackStackManager.clear();
        }
    }

    public final void setLaunchChildFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) {
        if (this.mChildFragmentBackStackManager == null || this.mChildFragmentBackStackManager.getAttachFragmentManager() != this.mFragment.getChildFragmentManager()) {
            this.mChildFragmentBackStackManager = new FragmentBackStackManager(this.mFragment.getChildFragmentManager());
        }
        this.mChildFragmentBackStackManager.setLaunchFragmentAttr(containerViewRes, enterAnimRes, popExitAnimRes);
    }

    public final void launchChildFragment(AbstractFragment childFragment) {
        assertChildFragmentBackStackManager();
        childFragment.getFragmentBackHelper().setFragmentBackStackManager(this.mChildFragmentBackStackManager);
        this.mChildFragmentBackStackManager.launchFragment(childFragment);
    }

    public void setFragmentBackStackManager(FragmentBackStackManager fragmentBackStackManager) {
        this.mParentFragmentBackStackManager = fragmentBackStackManager;
    }

    public final boolean isChildFragmentBackStackEmpty() {
        return this.mChildFragmentBackStackManager == null || this.mChildFragmentBackStackManager.isBackStackEmpty();
    }

    public final FragmentBackStackManager getChildFragmentBackStackManager() {
        return this.mChildFragmentBackStackManager;
    }

    public boolean onBackPressed() {
        if (this.mChildFragmentBackStackManager == null) {
            this.mFragment.finish();
            return true;
        } else if (!this.mChildFragmentBackStackManager.isBackStackEmpty()) {
            return this.mChildFragmentBackStackManager.getTopFragment().getFragmentBackHelper().onBackPressed();
        } else {
            this.mFragment.finish();
            return true;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isChildFragmentBackStackEmpty() || getChildFragmentBackStackManager().getTopFragment() == null) {
            return this.mFragment.onKeyDown(keyCode, event);
        }
        return getChildFragmentBackStackManager().getTopFragment().onKeyDown(keyCode, event);
    }

    public final void launchFragment(AbstractFragment fragment) {
        if (this.mFragment.isAdded()) {
            if (this.mParentFragmentBackStackManager == null) {
                this.mParentFragmentBackStackManager = ((AbstractActivity) this.mFragment.getActivity()).getFragmentBackStackManager();
            }
            fragment.getFragmentBackHelper().setFragmentBackStackManager(this.mParentFragmentBackStackManager);
            this.mParentFragmentBackStackManager.launchFragment(fragment);
        }
    }

    public final void launchFragment(AbstractFragment fragment, int containId, int enterAnimRes, int popExitAnimRes, int launchFlag) {
        if (this.mFragment.isAdded()) {
            fragment.getFragmentBackHelper().setFragmentBackStackManager(this.mParentFragmentBackStackManager);
            fragment.setLaunchFragmentFlag(launchFlag);
            this.mParentFragmentBackStackManager.launchFragment(fragment, containId, enterAnimRes, popExitAnimRes);
        }
    }

    public FragmentBackStackManager getParentFragmentBackStackManager() {
        return this.mParentFragmentBackStackManager;
    }

    public final void showPreviousFragment() {
        if (this.mParentFragmentBackStackManager != null) {
            this.mParentFragmentBackStackManager.showPreviousFragment();
        }
    }

    public final void hidePreviousFragment() {
        if (this.mParentFragmentBackStackManager != null) {
            this.mParentFragmentBackStackManager.hidePreviousFragment();
        }
    }

    private void assertChildFragmentBackStackManager() {
        if (Ext.g().isDebuggable() && this.mChildFragmentBackStackManager == null) {
            throw new IllegalStateException("you must call setLaunchChildFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) or attachChildFragmentBackStackManager(AbstractFragment fragment) first");
        }
    }
}
